﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using QFramework;

namespace HKTools
{
    /// <summary>
    /// 简易任务系统编辑器下的查看器
    /// </summary>
    public class HK_SimpleTaskViewer : EditorWindow, IController
    {
        public IArchitecture GetArchitecture()
        {
            return HK_ArchitectureProvider.Architecture;
        }

        Vector2 scrollPosition;

        // 缓存的 GUIStyle  
        GUIStyle titleLabelStyle;

        [MenuItem("HKTools/HK_SimpleTaskViewer(简易任务系统查看器)", priority = HK_MenuPriority.HK_SimpleTaskViewer)]
        public static void ShowWindow()
        {
            GetWindow<HK_SimpleTaskViewer>("HK_SimpleTaskViewer").Show();
        }

        void Awake()
        {
            // 初始化 yellowLabelStyle  
            titleLabelStyle = new GUIStyle(EditorStyles.boldLabel);
            titleLabelStyle.normal.textColor = Color.yellow;
        }

        void OnEnable()
        {
            this.RegisterEvent<Event_STS_TaskStart>(args =>
            {
                Repaint();
            });
            this.RegisterEvent<Event_STS_TaskFinish>(args =>
            {
                Repaint();
            });
        }

        void OnDisable()
        {
            this.UnRegisterEvent<Event_STS_TaskStart>(args =>
            {

            });
            this.UnRegisterEvent<Event_STS_TaskFinish>(args =>
            {

            });
        }

        void OnGUI()
        {
            // 只在运行时才能访问 简易任务系统的数据
            if (!Application.isPlaying)
            {
                EditorGUILayout.HelpBox("任务队列查看器仅在运行时可用。", MessageType.Warning);
                return;
            }

            IHK_SimpleTaskSystem taskSystem = this.GetSystem<IHK_SimpleTaskSystem>();

            if (taskSystem == null)
            {
                EditorGUILayout.HelpBox("简易任务系统未初始化。", MessageType.Error);
                return;
            }

            var taskQueueDic = taskSystem.TaskQueueDic;

            if (taskQueueDic == null || taskQueueDic.Count == 0)
            {
                EditorGUILayout.HelpBox("当前没有任务队列。", MessageType.Info);
                return;
            }

            EditorGUILayout.Space(5);

            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

            foreach (var kvp in taskQueueDic)
            {
                STS_TaskQueueType taskQueueType = kvp.Key;

                STS_QueueDataBase taskQueue = kvp.Value;

                EditorGUILayout.BeginVertical("box");

                EditorGUILayout.LabelField($"当前任务队列：", titleLabelStyle);

                EditorGUILayout.Space();

                EditorGUILayout.LabelField($"    {taskQueueType}");

                DrawHorizontalLine(Color.gray);

                EditorGUILayout.LabelField($"当前任务队列进度：", titleLabelStyle);

                EditorGUILayout.Space();

                EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), taskQueue.TaskProgress, $"{taskQueue.TaskProgress * 100}%");

                DrawHorizontalLine(Color.gray);

                EditorGUILayout.LabelField($"当前任务：", titleLabelStyle);

                EditorGUILayout.Space();

                EditorGUILayout.LabelField($"  →  {taskQueue.CurrentTaskName}");

                DrawHorizontalLine(Color.gray);

                //if (taskQueue.taskQueue == null)
                //    continue;

                List<string> pendingTasks = taskQueue.PendingTaskNames;
                List<string> finishedTasks = taskQueue.FinishedTaskNames;

                if (pendingTasks.Count > 0)
                {
                    EditorGUILayout.LabelField("已完成的任务：", titleLabelStyle);

                    EditorGUILayout.Space();

                    if (finishedTasks.Count == 0)
                    {
                        EditorGUILayout.LabelField($"● 无");
                    }
                    else
                    {
                        foreach (var task in finishedTasks)
                        {
                            EditorGUILayout.LabelField($"● {task}");
                        }
                    }

                    DrawHorizontalLine(Color.gray);

                    EditorGUILayout.LabelField("等待中的任务：", titleLabelStyle);

                    EditorGUILayout.Space();

                    foreach (var task in pendingTasks)
                    {
                        EditorGUILayout.LabelField($"● {task}");
                    }
                }
                else
                {
                    EditorGUILayout.LabelField("已完成的任务：", titleLabelStyle);

                    EditorGUILayout.Space();

                    foreach (var task in finishedTasks)
                    {
                        EditorGUILayout.LabelField($"● {task}");
                    }

                    DrawHorizontalLine(Color.gray);

                    EditorGUILayout.LabelField("等待中的任务：", titleLabelStyle);

                    EditorGUILayout.Space();

                    EditorGUILayout.LabelField("    没有等待中的任务");
                }

                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.EndScrollView();
        }

        void DrawHorizontalLine(Color color, int thickness = 1, int padding = 5)
        {
            EditorGUILayout.Space(5);

            Rect rect = EditorGUILayout.GetControlRect(false, thickness + padding);
            rect.height = thickness;
            rect.y += padding / 2;
            EditorGUI.DrawRect(rect, color);

            EditorGUILayout.Space(5);
        }
    }
}